#!/usr/bin/env bash
source /etc/multipool.conf
source $STORAGE_ROOT/yiimp/.yiimp.conf
#####################################################
# Dedicated Port config generator
# Created by cryptopool.builders for multipool
# This generator will modify the main algo.conf file
# Create the new coin.algo.conf file
# And update the stratum start file
#####################################################

clear

# Generate random open PORT
function EPHYMERAL_PORT(){
    LPORT=2768;
    UPORT=6999;
    while true; do
        MPORT=$[$LPORT + ($RANDOM % $UPORT)];
        (echo "" >/dev/tcp/127.0.0.1/${MPORT}) >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo $MPORT;
            return 0;
        fi
    done
}

coinport=$(EPHYMERAL_PORT)

cd /home/crypto-data/yiimp/site/stratum/config
echo "This program will create a new coin.algo.conf file and also create a new stratum start file."
echo "This program also randomly selects an open port for the coin between ports 2768 and 6999."
echo "This program will also open the new port in UFW."
echo ""
echo "Make sure coin symbol is all upper case."
read -e -p "Please enter the coin symbol : " coinsymbol
echo ""
echo "Make sure algo is in all lower case."
read -e -p "Please enter the coin algo : " coinalgo

# Make the coin symbol lower case

coinsymbollower=${coinsymbol,,}

# Copy the default algo.conf to the new coin.algo.conf
cp -r $coinalgo.conf $coinsymbollower.$coinalgo.conf

# Insert the port in to the new coin.algo.conf
sed -i '/port/c\port = '${coinport}'' $coinsymbollower.$coinalgo.conf

# Insert the include 
sed -i -e '$a\
[WALLETS]\
include = '${coinsymbol}'' $coinsymbollower.$coinalgo.conf

# Insert the exclude
sed -i -e '$a\
[WALLETS]\
exclude = '${coinsymbol}'' $coinalgo.conf


# New coin stratum start file

echo '#####################################################
# Source code from https://codereview.stackexchange.com/questions/55077/small-bash-script-to-sta$
# Updated by cryptopool.builders for crypto use...
#####################################################

source /etc/multipool.conf
source $STORAGE_ROOT/yiimp/.yiimp.conf
STRATUM_DIR=$STORAGE_ROOT/yiimp/site/stratum
LOG_DIR=$STORAGE_ROOT/yiimp/site/log
#!/usr/bin/env bash

'""''"${coinsymbollower}"''""'="screen -dmS '""''"${coinsymbollower}"''""' bash $STRATUM_DIR/run.sh '""''"${coinsymbollower}"''""'.'""''"${coinalgo}"''""'"
'""''"${coinsymbollower}"''""'stop="'screen -X -S ${coinsymbollower} quit'"

startstop_'""''"${coinsymbollower}"''""'() {
    cmd=$1
    case $cmd in
        stop) $'""''"${coinsymbollower}"''""'stop ;;
        start) $'""''"${coinsymbollower}"''""' ;;
        restart)
            $'""''"${coinsymbollower}"''""'stop
            sleep 1
            $'""''"${coinsymbollower}"''""'
            ;;
    esac
}

case "$1" in
    start|stop|restart) cmd=$1 ;;
    *)
        shift
        servicenames=${@-servicenames}
        echo "usage: $0 [start|stop|restart] algo"
        exit 1
esac


shift

for name; do
    case "$name" in
    '""''"${coinsymbollower}"''""') startstop_'""''"${coinsymbollower}"''""' $cmd ;;
    *) startstop_service $cmd $name ;;
    esac
done ' | sudo -E tee $STORAGE_ROOT/yiimp/site/stratum/config/stratum.${coinsymbollower} >/dev/null 2>&1
sudo chmod +x $STORAGE_ROOT/yiimp/site/stratum/config/stratum.${coinsymbollower}

sudo cp -r stratum.${coinsymbollower} /usr/bin 
sudo ufw allow $coinport

echo "To use your new start file type stratum.${coinsymbollower} start|stop|restart ${coinsymbollower}"
echo "To see the screen type screen -r ${coinsymbollower}"
echo "Make sure that you update your coin.conf on your DAEMON server for blocknotify to use this new port : $coinport"
echo "If you do not update this you will get blocknotify errors!"

cd ~
exit 0
